package es.udc.pfc.davidparedes.web.pages.user;

import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.ioc.annotations.Inject;

import es.udc.pfc.davidparedes.model.store.Store;
import es.udc.pfc.davidparedes.model.storeservice.StoreService;
import es.udc.pfc.davidparedes.model.util.InstanceNotFoundException;
import es.udc.pfc.davidparedes.web.services.AuthenticationPolicy;
import es.udc.pfc.davidparedes.web.services.AuthenticationPolicyType;
import es.udc.pfc.davidparedes.web.util.UserSession;

@AuthenticationPolicy(AuthenticationPolicyType.ADMIN)
public class UpdateStore {
	
    @Property
    private String name;
    
    @Property
    private String address;
    
    @Property
    private Long storeId;
	
    @SessionState(create=false)
    private UserSession userSession;
    
    @Inject
    private StoreService storeService;
    
    @Component
    private Form updateStoreForm;
    
    void onActivate(Long id) {
    	try {
			Store store = storeService.findStore(id);
			storeId = id;
			name = store.getName();
			address = store.getAddress();
			
			
		} catch (InstanceNotFoundException e) {
		}
    	
    }
    
    Object onValidateFromUpdateStoreForm() {
    	
    	if (!updateStoreForm.isValid()) {
            return this;
        }
    	
    	try {
			storeService.findStore(storeId);
			storeService.updateStore(storeId, name, address);
			
		} catch (InstanceNotFoundException infe) {

		}
    	
    	return AdminStores.class;
    	
    }
    
}
